/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author indu.chawla
 */
import java.util.*;

public class TermDocumentMatrix {

	private Vector<String> documents = new Vector<String>();
	private Vector<String> uniqueTerms = new Vector<String>();
	private int numDocuments;
	Vector<String> Result_Vector_Array[];
	private double[][] TERM_DOCUMENT_MATRIX;
	
	public Vector<String> getUniqueTerms(){
		return uniqueTerms; 
	}
	
	public void setNumDocuments(int numDocuments){
		this.numDocuments = numDocuments;
		Result_Vector_Array = (Vector<String>[]) new Vector[numDocuments];
	}
	
	public void update(String document, Vector<String> terms, int index){
		/*Result_Vector_Array[index] = terms; 
		documents.addElement(document); 
		uniqueTerms.addAll(terms);
		uniqueTerms = new Vector<String>(new LinkedHashSet<String>(uniqueTerms));
		*/
            Result_Vector_Array[index]=new Vector<String>();
                Result_Vector_Array[index].addAll(terms);
            
            // Vector<String> demo = Result_Vector_Array[index];
		documents.addElement(document); 
                 
		uniqueTerms.addAll(terms);
		uniqueTerms = new Vector<String>(new LinkedHashSet<String>(uniqueTerms));
		//Enumeration VEnum=documents.elements();
                /*System.out.println("elements in vector");
                while( VEnum.hasMoreElements())
                        System.out.print(VEnum.nextElement()+" ");
                        System.out.println();
                        System.out.println("index" +index);
                        System.out.println();*/
                      /* Enumeration VVnum=uniqueTerms.elements();
                System.out.println("uniqueterms ");
                while( VVnum.hasMoreElements())
                { System.out.print(VVnum.nextElement()+" ");
                        System.out.println();}*/
	}
        public Vector<String> getDocuments(){
		return documents;
	}
	
	public double[][] getTermDocumentMatrix(){
		return TERM_DOCUMENT_MATRIX;
	}
	
	public void processTermDocumentMatrix(){
		/*Collections.sort(uniqueTerms);
		TERM_DOCUMENT_MATRIX = new double[uniqueTerms.size()][documents.size()];
		for(int i=0;i<Result_Vector_Array.length;i++){
			Vector<String> identifiers = Result_Vector_Array[i];
			for(int j=0;j<identifiers.size();j++){
				String identifier = identifiers.elementAt(j);
				int index = uniqueTerms.lastIndexOf(identifier);
				TERM_DOCUMENT_MATRIX[index][i] = TERM_DOCUMENT_MATRIX[index][i] + 1;  
			}
		}*/
            Collections.sort(uniqueTerms);
		TERM_DOCUMENT_MATRIX = new double[uniqueTerms.size()][documents.size()];
                System.out.println("uniqueTerms.size()"+ uniqueTerms.size());
                System.out.println("documents.size()"+ documents.size());
                System.out.println("Result_Vector_Array.length" +Result_Vector_Array.length);
                 for(int i=0;i<Result_Vector_Array.length;i++){
                    Vector<String> identifiers = Result_Vector_Array[i];
                 for(int j=0;j<identifiers.size();j++){
				String identifier = identifiers.elementAt(j);
				int index = uniqueTerms.lastIndexOf(identifier);
				TERM_DOCUMENT_MATRIX[index][i] = TERM_DOCUMENT_MATRIX[index][i] + 1;  
			//System.out.print("identifiers" + identifiers.elementAt(j));
                 } } for(int k=0;k<uniqueTerms.size();k++){
			for(int m=0;m<documents.size();m++){
			System.out.print(   TERM_DOCUMENT_MATRIX[k][m]+"   " );
                        } 
                       System.out.println(" ");
	}
        }}

